package com.turing.post.log.util;

import java.util.logging.Filter;
import java.util.logging.LogRecord;

import javax.naming.InitialContext;

import weblogic.logging.WLLogRecord;
import weblogic.workarea.WorkContext;
import weblogic.workarea.WorkContextMap;

public class WLSFilter implements Filter {
	private WorkContextMap map = null;

	public String getContext() {
		synchronized (WLSFilter.class) {
			if (map == null) {
				try {
					map = (WorkContextMap) new InitialContext()
							.lookup("java:comp/WorkContextMap");
				} catch (Exception ex) {
				}
			}
		}

		if (map != null) {
			WorkContext stringContext = map.get(PCPSLogHandler.PCPSCONTEXT);
			if (stringContext != null)
				return stringContext.toString();
		}
		return null;
	}

	public boolean isLoggable(LogRecord record) {
		if (record instanceof WLLogRecord) {
			String scontext = getContext();
			if (scontext == null || scontext.trim().length() < 1)
				return true;
			return false;
		}
		return true;
	}
}